package com.jdew.thkj.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.activity.KeFuActivity;
import com.jdew.thkj.activity.MainActivity;
import com.jdew.thkj.activity.MainSearchActivity;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.view.NoScrollViewPager;
import com.jdew.thkj.view.Reflex;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;


/**
 * Created by yangyang on 2017/11/17.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewpager)
    NoScrollViewPager viewpager;
    @Bind(R.id.sosuo)
    TextView search;
    @Bind(R.id.refresh)
    TextView refresh;
    @Bind(R.id.kefu)
    ImageView kefu;
    @Bind(R.id.tlh)
    ImageView tlh;
    @Bind(R.id.loading)
    public RelativeLayout loading;
    @Bind(R.id.nonetwork_view)
    RelativeLayout nonetwork;
    private LongPagerAdapter adapter;
    private MainActivity activity;

    @Override
    protected int getContentViewId() {
        return R.layout.main_home;
    }

    @Override
    protected void onCreateViewT() {
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    private void initView() {
        activity = (MainActivity) getActivity();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        adapter = new LongPagerAdapter(getChildFragmentManager());
        viewpager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewpager);
        tlh.setVisibility(View.VISIBLE);
        kefu.setVisibility(View.VISIBLE);
        kefu.setOnClickListener(this);
        Reflex.setTabWidth(tabLayout, 50);
        search.setOnClickListener(this);
        refresh.setOnClickListener(this);
        if (!DeviceUtils.isNetworkAvalible(mActivity)) {
            if (!mActivity.isFinishing()) {
                nonetwork.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sosuo: {
//                activity.viewpager.setCurrentItem(1);
//                activity.rb = (RadioButton) activity.rgp.getChildAt(1);
//                activity.rb.setChecked(true);
                startActivity(MainSearchActivity.class);
            }
            break;
            case R.id.kefu: {
                startActivity(KeFuActivity.class);
            }
            break;
            case R.id.refresh: {
                EventBus.getDefault().post("refreshnonetwork");
            }
            break;
            default:
                break;
        }
    }


    private class LongPagerAdapter extends FragmentPagerAdapter {
        public String[] mTilte;

        public LongPagerAdapter(FragmentManager fm) {
            super(fm);
            mTilte = getResources().getStringArray(R.array.tab_sorts_Title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTilte[position];
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = FragmentFactory.createFragment6(position);
            return fragment;
        }

        @Override
        public int getCount() {
            return mTilte.length;
        }
    }

    @Override
    public void initData() {
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String source) {
        if (source.equals("finishloading")) {
            loading.setVisibility(View.GONE);
        } else if (source.equals("toHome")) {
            viewpager.setCurrentItem(0);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }
}
